import util from '../../utils/util.js'
import {
  getOilGunModel
} from '../../models/method.js'
import {
  getOilOrderModel
} from '../../models/orders.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    key: {},
    oil: '',
    money: '',
    numMoney: ['100', '400', '600', '800'],
    oilList: [],
    gunId: '',
    isPayMent: false
  },
  //生命周期函数--监听页面加载
  onLoad(e) {
    wx.setNavigationBarTitle({
      title: e.merchant_name
    })
    console.log(e.merchant_name)
    this.clickMoney()
    this.getGuns(e.merchant_id)
  },
  //点击金额
  clickMoney() {
    const key = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '清空', '0', '.']
    this.setData({
      key,
    })
  },
  // 点击金额下面的数字
  clickMoneyNumber(event) {
    const oil = this.data.oil
    const money = event.target.dataset.text
    this.setData({
      money,
    })
    //判断样式
    this.enterPayMent(oil, money)
  },
  //点击油枪下面的油枪
  clickOilList(event) {
    const money = this.data.money;
    const oil = event.target.dataset.text;
    const gunId = event.target.dataset.id;
    this.setData({
      oil,
      gunId,
    });
    //判断样式
    this.enterPayMent(oil, money)
  },
  //点击键盘
  clickKey(event) {
    const num = event.target.dataset.text
    let oil = this.data.oil
    let money = this.data.money
    //输入油枪
    money += num
    if (num == '清空') {
      money = ''
    }
    this.setData({
      money
    })
    //是否确认支付
    this.enterPayMent(oil, money)

  },
  //确认支付是否进行
  enterPayMent(oil, money) {
    // if (oil && money >= 10) { //测试
    if (oil && money > 0) {
      this.setData({
        isPayMent: true
      })
    } else {
      this.setData({
        isPayMent: false
      })
    }
  },
  //删除输入框
  clickDelete() {
    let oil = this.data.oil
    let money = this.data.money
    //删除金额
    money = money.slice(0, -1)
    this.setData({
      money
    })
    //是否确认支付
    this.enterPayMent(oil, money)
  },
  //创建预油品订单
  createOrder() {
    const id = this.data.gunId
    const money = this.data.money
    console.log(id, money)
    if (id == '') {
      return util.showToast('尚未选择油枪')
    }
    if (money == '') {
      return util.showToast('尚未填写加油金额')
    }
    getOilOrderModel(id, money).then((res) => {
      if (res.errorCode == 0) {
        const data = res.data
        const item = JSON.stringify(data)
        wx.navigateTo({
          url: `../payment/payment?item=${item}`
        })
      } else {
        util.showToast('内部系统错误！')
      }
    })
  },
  // 获取商户油枪
  getGuns(id) {
    getOilGunModel(id).then((res) => {
      this.setData({
        oilList: res.data.list
      })
      console.log(this.data.oilList)
    })
  },
  // 分享
  onShareAppMessage() {

  }
})